package com.anti.modular.bus.busrealestatefloor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.commom.api.Result;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.commom.core.exception.BusinessException;
import com.anti.jdbc.support.page.PageResult;
import com.anti.modular.bus.busrealestatefloor.entity.BusRealEstateFloor;
import com.anti.modular.bus.busrealestatefloor.enums.BusRealEstateFloorExceptionEnum;
import com.anti.modular.bus.busrealestatefloor.param.BusRealEstateFloorParam;
import com.anti.modular.bus.busrealestatefloor.result.FloorListResult;
import com.anti.modular.bus.busrealestatefloor.service.BusRealEstateFloorService;
import com.anti.modular.bus.busroom.service.BusRoomService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 房产楼层表service接口实现类
 *
 * @author anti
 * @date 2021-04-15 19:15:53
 */
@Service
public class BusRealEstateFloorServiceImpl implements BusRealEstateFloorService {
    @Resource
    DBUtils dbUtils;
    @Resource
    BusRoomService busRoomService;

    @Override
    public BusRealEstateFloor getById(BusRealEstateFloorParam busRealEstateFloorParam){
        String sql="select * from bus_real_estate_floor where id =? ";
        return dbUtils.get(sql,BusRealEstateFloor.class,busRealEstateFloorParam.getId());
    }

    @Override
    public BusRealEstateFloor getFloorById(Long id) {
        String sql="select * from bus_real_estate_floor where id =? ";
        return dbUtils.getFirst(sql,BusRealEstateFloor.class,id);
    }

    @Override
    public PageResult page(PageParam pageParam, BusRealEstateFloorParam busRealEstateFloorParam) {
        StringBuilder sb=new StringBuilder("select * from bus_real_estate_floor where 1=1 and status=0");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(busRealEstateFloorParam)) {
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public ArrayList<FloorListResult> list(String estateid) {
        String sql="select * from bus_real_estate_floor where status=0 and estateid=?";
        List<BusRealEstateFloor> mapList = dbUtils.query(sql,BusRealEstateFloor.class,estateid);
        ArrayList<FloorListResult> arrayList = CollectionUtil.newArrayList();
        FloorListResult floorListResult=null;
        for (int i = 1; i <100 ; i++) {
            floorListResult=new FloorListResult();
            floorListResult.setCode(i);
            for (int a = 0; a <mapList.size() ; a++) {
                if (mapList.get(a).getCode().equals(i)){
                    floorListResult.setDisabled(true);
                    break;
                }
            }
            arrayList.add(floorListResult);
        }
        return arrayList;
    }

    @Override
    public List<FloorListResult> busRealEstateFloorListForHave(String estateid) {
        String sql="select id,code,name from bus_real_estate_floor where status=0 and estateid=?";
        List<FloorListResult> mapList = dbUtils.query(sql,FloorListResult.class,estateid);
        return mapList;
    }

    @Override
    public void add(BusRealEstateFloorParam busRealEstateFloorParam) {
        String sql="select count(*) from bus_real_estate_floor where status = 0 and estateid=? and code =?";
        int intValue = dbUtils.getIntValue(sql, busRealEstateFloorParam.getEstateid(), busRealEstateFloorParam.getCode());
        if (intValue>0){
            throw  new BusinessException(0,"該樓層已存在");
        }
        BusRealEstateFloor busRealEstateFloor = new BusRealEstateFloor();
        BeanUtil.copyProperties(busRealEstateFloorParam, busRealEstateFloor);
        busRealEstateFloor.setCreateTime(new Date());
        busRealEstateFloor.setName(busRealEstateFloorParam.getCode()+"樓");
        busRealEstateFloor.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        busRealEstateFloor.setStatus(0);
        dbUtils.insert(busRealEstateFloor);
    }


    public Result delete(BusRealEstateFloorParam busRealEstateFloorParam) {
        int countByFloorId = busRoomService.getRoomRentCountByFloorId(busRealEstateFloorParam.getId());
        if (countByFloorId>0){
            return Result.fail(0,"該房產有在租租約，請先退租");
        }
        //判断是否有预定

        String sql="update  bus_real_estate_floor set status=2,update_time=?,update_user=? where id =? ";
        dbUtils.execSql(sql,new Date(),LoginContextHolder.me().getSysLoginUserId(),busRealEstateFloorParam.getId());
        busRoomService.deleteRoomByFloorId(busRealEstateFloorParam.getEstateid(),busRealEstateFloorParam.getId());
        return Result.success("刪除成功");
    }

    @Override
    public void deleteByFcId(Long id) {
        String sql="update  bus_real_estate_floor set status=2,update_time=?,update_user=? where estateid =? ";
        dbUtils.execSql(sql,new Date(),LoginContextHolder.me().getSysLoginUserId(),id);
    }

    public void edit(BusRealEstateFloorParam busRealEstateFloorParam) {
        BusRealEstateFloor busRealEstateFloor = this.queryBusRealEstateFloor(busRealEstateFloorParam);
        BeanUtil.copyProperties(busRealEstateFloorParam, busRealEstateFloor);
        busRealEstateFloor.setUpdateTime(new Date());
        busRealEstateFloor.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.update(busRealEstateFloor);
    }

    @Override
    public BusRealEstateFloor detail(BusRealEstateFloorParam busRealEstateFloorParam) {
        return this.queryBusRealEstateFloor(busRealEstateFloorParam);
    }

    /**
     * 获取房产楼层表
     *
     * @author anti
     * @date 2021-04-15 19:15:53
     */
    private BusRealEstateFloor queryBusRealEstateFloor(BusRealEstateFloorParam busRealEstateFloorParam) {
        BusRealEstateFloor busRealEstateFloor = this.getById(busRealEstateFloorParam);
        if (ObjectUtil.isNull(busRealEstateFloor)) {
            throw new BusinessException(BusRealEstateFloorExceptionEnum.NOT_EXIST);
        }
        return busRealEstateFloor;
    }


}
